package apig.client.stud;

import apig.client.*;
import apig.client.serviface.*;

import com.google.gwt.core.client.*;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.user.client.*;
import com.google.gwt.user.client.rpc.*;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;

public class StudEntryPoint implements EntryPoint {

	private UserData ud;
	private StudHeader header;
	private StudContentPanel content;
	private int selectedLabId;

	public void onModuleLoad() {

		Stubman.getStubman().getService()
				.getUserData(new AsyncCallback<UserData>() {

					@Override
					public void onFailure(Throwable caught) {
						onGetDataFailed(caught.getMessage());
					}

					@Override
					public void onSuccess(UserData result) {
						onGetDataSuccess(result);
					}
				});
	}

	private void onLoadUi() {
		RootPanel rlp = RootPanel.get("container");
		VerticalPanel vp = new VerticalPanel();
		vp.setWidth("100%");
		vp.setHeight("100%");
		vp.setBorderWidth(5);
		header = new StudHeader(ud);
		vp.add(header);
		rlp.add(vp);

		content = new StudContentPanel(ud);
		content.setHeight("100%");
		content.setWidth("100%");

		StudDiscsView ssp = new StudDiscsView(ud, content,
				new LabSelectionHandler());
		ssp.setHeight("100%");
		ssp.setWidth("100%");

		VerticalPanel leftPanel = new VerticalPanel();
		leftPanel.setWidth("100%");
		leftPanel.setHeight("100%");
		leftPanel.add(new UploadPanel());
		leftPanel.add(ssp);

		VerticalPanel stub = new VerticalPanel();
		stub.setWidth("100%");
		stub.setHeight("100%");
		leftPanel.setCellWidth(stub, "100%");
		leftPanel.setCellHeight(stub, "100%");

		HorizontalPanel splitPanel = new HorizontalPanel();
		splitPanel.add(leftPanel);
		splitPanel.add(content);
		splitPanel.setCellWidth(leftPanel, "20%");
		splitPanel.setCellHeight(leftPanel, "100%");
		splitPanel.setCellWidth(content, "80%");
		splitPanel.setWidth("100%");
		splitPanel.setHeight("100%");
		splitPanel.setBorderWidth(2);
		vp.add(splitPanel);

		// vp.setCellHeight(header, "100%");
		vp.setCellHeight(splitPanel, "100%");
	}

	private int getSelectedLabId() {
		return selectedLabId;
	}

	private class UploadPanel extends FormPanel {

		private FileUpload fu = new FileUpload();

		public UploadPanel() {
			setEncoding(FormPanel.ENCODING_MULTIPART);
			setMethod(FormPanel.METHOD_POST);
			

			VerticalPanel vp = new VerticalPanel();

			fu.setName("upload");
			vp.add(fu);
			vp.add(new Button("Загрустить", new ClickHandler() {
				public void onClick(ClickEvent event) {
					System.out.println("You selected: " + fu.getFilename());
					if (getSelectedLabId() != 0) {
						setAction("/fileupload?" + Stubman.LAB_ID + "="
								+ getSelectedLabId());
						submit();
					} else {
						Window.alert("Не выбрана лаба. Выберите её, пожалуйста, чтобы загружать код.");
					}
				}
			}));

			addSubmitHandler(new FormPanel.SubmitHandler() {
				public void onSubmit(SubmitEvent event) {
					if ("".equalsIgnoreCase(fu.getFilename())) {
						event.cancel();
					}

				}
			});

			addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
				public void onSubmitComplete(SubmitCompleteEvent event) {
					content.reloadContent();
					Window.alert("Загрузка завершена");
				}
			});

			add(vp);
		}
	}

	private void onGetDataSuccess(UserData result) {
		System.out.println("on get data success");
		if (result != null && result.logged
				&& result.role == UserData.ROLE_STUD) {
			ud = result;
			onLoadUi();
		} else {
			System.out.println("unauthorized");
		}
	}

	private void onGetDataFailed(String msg) {
		System.out.println("fail: " + msg);
	}

	private class LabSelectionHandler implements LabSelectionListener {
		@Override
		public void onLabSelected(int labId) {
			selectedLabId = labId;
		}
	}
}
